package com.zcurd.controller;

import java.util.List;

import com.busi.util.PropertyUtil;
import com.zcurd.common.util.SlpMD5;
import com.zcurd.model.User;


/**
 * ******************  类说明  *********************
 * class       :  UserController
 * @author     :  薛泽荣
 * @version    :  1.0  
 * description :  用户Controller
 * @see        :                        
 * ***********************************************
 */
public class UserController extends BaseController {
	
	public void toChangePwd(){
		setAttr("model", getSessionAttr("sysUser"));
		render("changepwd.html");
	}
	
	public void changePwd(){
		String id = getPara("model.id");
		String old_password = getPara("model.old_password");
		String password = getPara("model.password");
		LOG.info(id+","+old_password+","+password);
		List<User> list = User.me.findByMultiProperties(new String[]{"id", "password"}, 
				new Object[]{id, SlpMD5.generateCheckCode(old_password)});
		if ( (null != list) && (list.size() > 0)){
			User user = new User();
			user.set("id", id);
			user.set("password", SlpMD5.generateCheckCode(password));
			if (user.update()){
				addOpLog("[修改密码] 成功");
				renderSuccess();
				return ;
			}
		}
		addOpLog("[修改密码] 失败");
		renderFailed("旧密码输入错误，密码修改失败！");
	}
	/**
	 * ********************************************
	 * method name   : resetPwd 
	 * description   : 密码重置
	 * @return       : void
	 * @param        : 
	 * modified      : xuezerong@umpay.com ,  2016-12-8  上午10:30:36
	 * @see          : 
	 * *******************************************
	 */
	public void resetPwd(){
		User user = User.me.findById(getParaToInt("id"));
		String username = "";
		LOG.info("#resetPwd() 初始化商户密码，商户信息：" + user);
		if(user != null){
			username = user.getStr("user_name");
			String password = PropertyUtil.getStrValue("merinf.initial.pwd", "");
			user.set("password", SlpMD5.generateCheckCode(password+username));
			if (user.update()){
				LOG.info("#resetPwd() 初始化商户密码成功，商户信息：" + user);
				addOpLog("[商户:" + username + "密码初始化] 成功");
				renderSuccess();
				return ;
			}
		}
		LOG.info("#resetPwd() 初始化商户密码失败，商户信息：" + user);
		addOpLog("[商户:" + username + "密码初始化]失败");
		renderFailed("商户密码初始化失败！");
	}
}
